package com.hanliy.controller;

import com.hanliy.dto.Result;
import com.hanliy.dto.UserDTO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * @author hanyulin
 * @apiNote 测试路径参数、方法参数、对象校验
 * @date 2021/12/12 16:50
 */
@RestController
@RequestMapping(("/user"))
@Validated
public class UserController {

    /**
     * 测试对象校验
     *
     * @param userDTO UserDTO
     * @author hanyulin
     * @date 2021/12/12 22:26
     * @return Result
     */
    @PostMapping
    public Result save(@RequestBody @Validated UserDTO userDTO) {
        return new Result().ok();
    }

    /**
     * 测试方法校验，一定要在类上加入@Validated注解
     *
     * @param param 参数
     * @author hanyulin
     * @date 2021/12/12 22:26
     * @return Result
     */
    @GetMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "参数", required = false, dataType = "String", paramType = "query")
    })
    public Result getUser(@Length(min = 6, max = 20) @NotNull @RequestParam String param) {
        return new Result().ok();
    }

    /**
     * 测试路径校验，一定要在类上加入@Validated注解
     *
     * @param id 路径参数
     * @author hanyulin
     * @date 2021/12/12 22:27
     * @return Result
     */
    @GetMapping(value = "/{id}")
    public Result detail(@PathVariable("id") @Min(100000000000000000L) Long id) {
        return new Result().ok();
    }

}





